extern crate esb_xml;
extern crate serde;

use std::io::Read;

use crate::esb::head::{AppHead, Head, LocalHead, SysHead};
use crate::esb::svr_fms_27270002::{Body270002, Hold, Service, Zlc270002};

mod esb;

#[test]
fn test_serialize() {
    let src = Service {
        sys: SysHead {
            server_id: Some("127.0.0.1".into()),
            service_code: None,
            message_type: None,
            user_id: None,
            tran_timestamp: None,
            consumer_id: None,
            ws_id: None,
            seq_no: None,
            tran_mode: None,
            source_type: None,
            user_lang: None,
            message_code: None,
            module_id: None,
            tran_date: None,
            branch_id: None,
            tran_code: None,
            source_branch_no: None,
            dest_branch_no: None,
            customer_id: None,
            ret_status: None,
            ret: Some(vec![esb::head::Ret {
                code: Some("000000".into()),
                msg: Some("".into()),
            }]),
        },
        app: AppHead {
            page_up_down: None,
            total_num: None,
            current_num: None,
            page_start: None,
            page_end: None,
            total_rows: None,
            total_pages: None,
            total_flag: None,
            approve_flag: None,
            approve_user_id: None,
            auth_flag: None,
            auth_user_id: None,
            file_path: None,
            program_id: None,
            reversal_tran_type: None,
            tran_type: None,
            operation_code: None,
        },
        local: LocalHead { ret: None },
        body: Zlc270002 {
            system_work_date: Some("20200527".into()),
            hold_array: Some(vec![Hold {
                order_no: Some("RN202009160007J6".into()),
                prod_code: None,
                prod_name: None,
                min_hold_day: None,
                buy_amount: None,
                hold_amount: None,
                apply_amount: None,
                original_start_rate_date: None,
                start_rate_date: None,
                buy_date: None,
                end_date: None,
                order_transferable_date: None,
                basic_rate: None,
                frozen_amount: None,
                order_status: None,
                provision_date: None,
                provision_interest: None,
                term: None,
                term_type: None,
                saved_days: None,
                last_hold_amt: None,
                remain_full_interest: None,
                remain_days: None,
                sig_minbuyamt: None,
                draw_min_amount: None,
                draw_step_amount: None,
                transfer_minbuyamt: None,
                buy_stepamt: None,
                widthdraw_substract_bankpay: None,
                current_rate: None,
                draw_frozen_amount: None,
                total_bank_pay_interest: None,
                seckill_status: None,
                prod_class: None,
                period_pay_interest_days: None,
                current_period_start_date: None,
                current_period_end_date: None,
                current_period_get_amount: None,
                current_period_expect_end_date: None,
            }]),
        },
    };

    let got = esb_xml::se::to_string(&src).unwrap();
    println!("编码：{}", got);
}

#[test]
fn test_deserialize() {
    let mut file = std::fs::File::open("./tests/xml/temp.xml").unwrap();
    let mut content = String::new();
    file.read_to_string(&mut content).unwrap();

    let head: Result<Head, esb_xml::DeError> = esb_xml::de::from_str(&*content);
    println!("解码：{:#?}", head);

    let body: Result<Body270002, esb_xml::DeError> = esb_xml::de::from_str(&*content);
    println!("解码：{:#?}", body);

    let msg: Result<Service, esb_xml::DeError> = esb_xml::de::from_str(&*content);
    println!("解码：{:#?}", msg);
}
